package zabbix

import (
	"encoding/json"
	"errors"
)

func MediatypeCreateData() (string,map[string]interface{}) {
	method := "mediatype.create"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "type": 1,
        "description": "Push notifications",
        "exec_path": "push-notification.sh",
        "exec_params": "{ALERT.SENDTO}\n{ALERT.SUBJECT}\n{ALERT.MESSAGE}\n",
        "maxattempts": "5",
        "attempt_interval": "11s"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func MediatypeDeleteData() (string,map[string]interface{}) {
	method := "mediatype.delete"
	var data map[string]interface{}
	jsonstr := []byte(`[
        "3",
        "5"
	]`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func MediatypeUpdateData() (string,map[string]interface{}) {
	method := "mediatype.update"
	var data map[string]interface{}
	jsonstr := []byte(`[
        "mediatypeid": "6",
        "status": 0
	]`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func MediatypeGetData() (string,map[string]interface{}) {
	method := "mediatype.get"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "output": "extend"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}


func MediatypeCreate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := MediatypeCreateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func MediatypeDelete(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := MediatypeDeleteData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func MediatypeUpdate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := MediatypeUpdateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func MediatypeGet(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := MediatypeGetData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}



